package com.lh.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh.enums.YesOrNo;
import com.lh.mapper.ItemsCommentsExtMapper;
import com.lh.mapper.ItemsCommentsMapper;
import com.lh.mapper.OrderItemsMapper;
import com.lh.mapper.OrderStatusMapper;
import com.lh.mapper.OrdersMapper;
import com.lh.pojo.OrderItems;
import com.lh.pojo.OrderStatus;
import com.lh.pojo.Orders;
import com.lh.pojo.bo.center.OrderItemsCommentBO;
import com.lh.pojo.vo.MyCommentVO;
import com.lh.service.center.MyCommentsService;
import com.lh.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-30 10:04
 **/
@Service
public class MyCommentsServiceImpl implements MyCommentsService {

    @Autowired
    private OrderItemsMapper orderItemsMapper;
    @Autowired
    private Sid sid;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;
    @Autowired
    private ItemsCommentsExtMapper itemsCommentsExtMapper;
    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<OrderItems> pending(String orderId) {

        OrderItems orderItems = new OrderItems();
        orderItems.setOrderId(orderId);

        return orderItemsMapper.select(orderItems);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveList(String userId, String orderId, List<OrderItemsCommentBO> commentList) {

        // 1. 保存评价 items_comments
        for (OrderItemsCommentBO oic : commentList) {
            oic.setCommentId(sid.nextShort());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("commentList", commentList);
        itemsCommentsExtMapper.saveComments(map);

        // 2. 修改订单表改已评价 orders
        Orders order = new Orders();
        order.setId(orderId);
        order.setIsComment(YesOrNo.YES.getType());
        ordersMapper.updateByPrimaryKeySelective(order);

        // 3. 修改订单状态表的留言时间 order_status
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderId(orderId);
        orderStatus.setCommentTime(new Date());
        orderStatusMapper.updateByPrimaryKeySelective(orderStatus);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult query(String userId, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        List<MyCommentVO> myCommentVOList = itemsCommentsExtMapper.getCommentByUserId(userId);
        PagedGridResult pageData = getPageData(myCommentVOList, page);
        return pageData;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult getPageData(List<?> list, Integer page){
        PageInfo<?> pageList = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
